package cn.fjy.android.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import android.util.Log;

/**
 * 配置文件操作工具类
 * @author GrayF
 * @2011-3-30
 */
public class GRPropertiesUtil {
	
	private String tag = "GRPropertiesUtil";
	
	private Properties prop;
	
	private String fileBasePath = "/mnt/sdcard/";
	
	private String configFileParentDir = ".GRPhoneAPP/";
	
	private String conFileParentDirRealPath = fileBasePath + configFileParentDir;
	
	private String configFileExt = ".g_recording";
	
	private File configFile;
	
	@SuppressWarnings("unused")
	private GRPropertiesUtil(){}
	
	/**
	 * 加载配置文件，如不存在则创建新文件
	 * @param fileName
	 */
	public GRPropertiesUtil(String fileName){
		configFile = checkConfFileExist(fileName);
		if(configFile!=null){
			loadFile(configFile);
		}else{
			createFile(fileName);
		}
	}
	
	/**
	 * 获取key值
	 * @param key
	 * @return
	 */
	public String getValue(String key){
		String str = "";
		if(prop!=null){
			str = prop.getProperty(key);
		}
		return str;
	}
	
	/**
	 * 设置某个键的值
	 * @param key 键
	 * @param value 值
	 */
	public void setValue(String key,String value){
		if(prop!=null){
			prop.setProperty(key, value);
			try {
				prop.save(new FileOutputStream(configFile), "##GRPhoneAPPConfig##");
			} catch (FileNotFoundException e) {
				Log.e(tag, e.getMessage());
				return;
			}
		}
	}
	
	/**
	 * 添加一个键值对
	 * @param key 键
	 * @param value 值
	 */
	public void putValue(String key,String value){
		if(prop!=null){
			prop.put(key, value);
			try {
				prop.save(new FileOutputStream(configFile), "##GRPhoneAPPConfig##");
			} catch (FileNotFoundException e) {
				Log.e(tag, e.getMessage());
				return;
			}
		}
	}
	
	/**
	 * 创建空配置文件
	 * @param fileName 文件名，不含后缀名
	 * @return
	 */
	private void createFile(String fileName){
		if(!checkSDCardIsExist()){
			fileBasePath = "/tmp/";
		}
	
		File parentDir = new File(conFileParentDirRealPath);
		if(!parentDir.exists()){
			if(!parentDir.mkdir()){
				Log.e(tag,"创建配置文件父目录失败");
				return;
			}
		}
		configFile = new File(conFileParentDirRealPath+fileName+configFileExt);
		try {
			configFile.createNewFile();
		} catch (IOException e) {
			Log.e(tag, e.getMessage());
			return;
		}
		loadFile(configFile);
	}
	
	/**
	 * 创建Properties实例
	 * 
	 * @param file
	 */
	private void loadFile(File file){
		prop = new Properties();
		InputStream is;
		try {
			is = new FileInputStream(file);
			prop.load(is);
		} catch (Exception e) {
			Log.e(tag, e.getMessage());
			return ;
		}
	}
	
	/**
	 * 检测sd卡是否存在
	 * @return
	 */
	private boolean checkSDCardIsExist(){
		boolean result = false;
		File file = new File(fileBasePath);
		if(file.exists()&&file.isDirectory()){
			result = true;
		}else{
			conFileParentDirRealPath = "/tmp/"+configFileParentDir;
		}
		return result;
	}
	
	/**
	 * 检测配置文件是否存在
	 * @param fileName 文件名
	 * @return 存在则返回配置文件实例，否则返回NULL
	 */
	private File checkConfFileExist(String fileName){
		checkSDCardIsExist();
		File file = new File(conFileParentDirRealPath+fileName);
		if(file.exists()){
			return file;
		}
		return null;
	}

}
